﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ATFCodeCS
{
	public class ReadWrite
	{
		public StringBuilder Data { get; set; }// chuoi rong
		public List<Config> DSCF { get; set; }// danh sach  cac the 
		public List<Var> DSVAR { get; set; }
		public ReadWrite()
		{
			Data = new StringBuilder();
			DSCF = new List<Config>();
			DSVAR = new List<Var>();
		}
		public void writeData(string nameOutput)
		{
			FileInfo t = new FileInfo(nameOutput.ToString());
			StreamWriter Tex = null;
			try
			{
				Tex = t.CreateText();
				foreach (var item in DSVAR)
				{
					Tex.WriteLine(item.ToString());
				}
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
			}
			finally
			{
				Tex.Close();
			}
		}
		public void readData(string nameData)
		{
			StreamReader sr = null;
			int i = 0;
			try
			{
				sr = File.OpenText(nameData.ToString());
				string input = null;
				while ((input = sr.ReadLine()) != null)
				{
					Data.Append(input);
					Data.Append(Define.CRLF);

					i++;
				}
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
			}
			finally
			{
				sr.Close();
			}
		}
		private void setUpConfig(string[] dong , int n)
		{
			for (int i = 0; i < n; i++)
			{
				Config tmp = null;
				Var[] v = new Var[2];
				try
				{
					string[] str = dong[i].Split(Define.TAB);
					v[0] = new Var( str[0].ToString());
					v[1] = new Var(str[1].ToString());
					string[] strStart = str[0].Split(Define.START);
					
					tmp = new Config(v[0], v[1]);
					if (strStart.Length == 2)
					{
						tmp.coSao = true;
					}
					DSCF.Add(tmp);
				}
				catch (Exception ex)
				{
					System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
					return;
				}
			}
		}
		public void readConfig(string nameConfig)
		{
			string[] dong = new string[100];
			StreamReader sr = null;
			int i = 0;
			try
			{
				sr = File.OpenText(nameConfig.ToString());
				string input = null;
				while ((input = sr.ReadLine()) != null)
				{
					if (input.ToArray()[0].ToString().Equals(";"))
					{
					}
					else
					{
						dong[i] = input;
						i++;
					}
				}
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
			}
			finally
			{
				sr.Close();
			}
			setUpConfig(dong,i);
		}
	}
}